package com.ayoka.parenting.mvp;

import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ayoka.parenting.R;
import com.ayoka.parenting.utils.EmptyDataUtil;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.brvah.RecycleViewDivider;
import com.jincp.common.brvah.layoutmanager.MyLinearLayoutManager;

import java.util.List;

/**
 * =========================================================
 * @ProjectName:
 * @ClassName: MVPBaseRecyclerViewActivity
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/10/22 11:46
 * =========================================================
 */
public abstract class MVPBaseRecyclerViewActivity<T, VDB extends ViewDataBinding, V extends BaseView, P extends BasePresenter> extends MVPBaseActivity<VDB, V, P> implements BaseQuickAdapter.RequestLoadMoreListener,
        SwipeRefreshLayout.OnRefreshListener {


    protected SwipeRefreshLayout refreshLayout;// 下拉刷新

    protected RecyclerView recyclerView;
    protected BaseQuickAdapter<T, BaseViewHolder> adapter;

    protected int pageNo = 1;
    protected int pageSize = 10;
    protected boolean isRefreshFlag;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_base_recyclerview;
    }

    protected void initViews() {
        recyclerView = getRecyclerView();
        refreshLayout = getRefreshLayout();
        if (recyclerView != null) {
            initRecyclerView();
        }
        if (refreshLayout != null) {
            initRefreshView();
        }
    }

    protected abstract SwipeRefreshLayout getRefreshLayout();

    protected abstract RecyclerView getRecyclerView();

    protected void initRecyclerView() {
        recyclerView.setLayoutManager(getLayoutManager());
        adapter = getAdapter();
        adapter.bindToRecyclerView(recyclerView);
        if (getLoadEnable()) {
            adapter.setOnLoadMoreListener(this, recyclerView);
        }
        if (getRecycleDivider() != null) {
            recyclerView.addItemDecoration(getRecycleDivider());
        }
    }

    private void initRefreshView() {
        if (getRefreshEnable()) {
            if (refreshLayout != null) {
                refreshLayout.setEnabled(true);
                refreshLayout.setOnRefreshListener(this);
            }
        } else {
            if (refreshLayout != null) {
                refreshLayout.setEnabled(false);
            }
        }
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new MyLinearLayoutManager(this);
    }

    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return RecycleViewDivider.getLineDivider(this);
    }

    protected boolean getRefreshEnable() {
        return true;
    }

    protected boolean getLoadEnable() {
        return true;
    }

    @NonNull
    protected abstract BaseQuickAdapter<T, BaseViewHolder> getAdapter();

    protected abstract void getData(boolean isShowWaitDialog);

    @Override
    public void onLoadMoreRequested() {
        if (getLoadEnable()) {
            pageNo++;
            isRefreshFlag = false;
            getData(false);
        }
    }

    @Override
    public void onRefresh() {
        if (getRefreshEnable()) {
            setSwipeRefreshLoadingState();
            pageNo = 1;
            isRefreshFlag = true;
            getData(true);
        }
    }


    /**
     * 设置顶部正在加载的状态
     */
    protected void setSwipeRefreshLoadingState() {
        if (getRefreshEnable() && refreshLayout != null) {
            refreshLayout.setRefreshing(true);
            // 防止多次重复刷新
            refreshLayout.setEnabled(false);
        }
    }

    /**
     * 设置顶部加载完毕的状态
     */
    protected void setSwipeRefreshLoadedState() {
        if (getRefreshEnable() && refreshLayout != null) {
            refreshLayout.setRefreshing(false);
            refreshLayout.setEnabled(true);
        }
    }

    /**
     * 设置顶部加载完毕的状态
     */
    protected void setLoadMoreCompletedState() {
        if (adapter != null) {
            adapter.loadMoreComplete();
        }
    }

    protected void setLoadMoreLayout(List<T> data) {
        setLoadMoreLayout(data, pageNo);
    }

    protected void setLoadMoreLayout(List<T> data, int totalPage) {
        setSwipeRefreshLoadedState();
        setLoadMoreCompletedState();
        if (adapter == null || recyclerView == null) return;
        // 刷新下  清除原有数据
        if (isRefreshFlag) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }
        if (adapter.getData().isEmpty()) {
            if (getEmptyView() != null) {
                adapter.setEmptyView(getEmptyView());
            }
        } else {
            if (getLoadEnable()) {
                // 设置最大页数的话 执行判断loadMoreEnd
                if (pageNo >= totalPage) {
                    adapter.loadMoreEnd(false);
                }
            }
        }
    }

    protected View getEmptyView() {
        return EmptyDataUtil.getEmptyView(this, R.mipmap.icon_no_data, getString(R.string.empty_no_data), null);
    }
}
